package eom.example.elasticsearch.controller;

import bom.example.common.common.Results;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import eom.example.elasticsearch.domain.Student;
import eom.example.elasticsearch.domain.vo.StudentVo;
import eom.example.elasticsearch.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author X.I.O
 * @title: StudentController
 * @projectName spring-boot-elasticsearch
 * @description: TODO
 * @date 2021/11/17 10:04
 */
@Api("student测试模块")
@RequestMapping("/student")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;



    @ApiOperation("student分页")
    @PostMapping("page")
    public JSONObject pageStudentController(@ApiParam("页码")@RequestParam(value = "page",defaultValue = "1")Integer page,
                                            @ApiParam("条数")@RequestParam(value = "size",defaultValue = "10")Integer size,
                                            @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        IPage<Student> studentIPage=studentService.getPage(page,size,keyword);
        return Results.success(studentIPage.getRecords(),studentIPage.getTotal());
    }

    @ApiOperation("student新增")
    @PostMapping("add")
    public JSONObject addStudentController(@RequestBody @Validated StudentVo studentVo){
        Student student=new Student();
        BeanUtils.copyProperties(studentVo,student);
        studentService.save(student);
        return Results.success();
    }

    @ApiOperation("student删除")
    @PostMapping("/delete")
    public JSONObject deleteController(@RequestBody List<Integer> ids){
        if(ids==null || ids.size()==0){
            return Results.error("参数不得为空");
        }
        studentService.removeByIds(ids);
        return Results.success();
    }

    @ApiOperation("student更新")
    @PostMapping("/update")
    public JSONObject updateController(@RequestBody Student student){
        if(student==null || student.getId()==null){
            return Results.error("数据不存在,请刷新后再试");
        }
        Student student1=studentService.getById(student.getId());
        if(student1==null){
            return Results.error("数据不存在,请刷新后再试");
        }
        studentService.updateById(student);
        return Results.success();
    }

    @ApiOperation("stu获取单个")
    @PostMapping("get/{id}")
    public JSONObject getOneController(@ApiParam("id")@PathVariable("id")Integer id){
        Student student=studentService.getById(id);
        return Results.success(student);
    }

    @ApiOperation("添加stu数据到es")
    @PostMapping("/addtoes")
    public JSONObject addStudentToEsController(){
        List<Student> studentAll=studentService.list();
        String indexName="student";

       return null;
    }


}
